package com.wangji92.arthas.plugin.demo.test.http;

import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;

/**
 * @author liuxiongkang
 */
public class Client {
    private static final String BLANK = " ";    //空格符
    private static final String CRLF = "\r\n";  //回车换行符

    public static void main(String[] args) {
        try (Socket socket = new Socket("127.0.0.1", 8899)) {
            InputStream in = new BufferedInputStream(socket.getInputStream());      //获取输入流
            OutputStream out = new BufferedOutputStream(socket.getOutputStream());  //获取输出流

            StringBuilder msg = new StringBuilder();
            msg.append("FUCK").append(BLANK).append("/name=%E9%BE%99%E6%9E%97%E5%A4%95").append("HTTP/1.1").append(CRLF)//
					.append("User-Agent").append(": ").append("CrazyDragon").append(CRLF)//
					.append("accept").append(": ").append("*/*").append(CRLF)//
					.append("Host").append(": ").append("localhost:8899").append(CRLF)//
					.append("Content-Type").append(": ").append("application/x-www-form-urlencoded").append(CRLF)//
					.append("Content-Length").append(": ").append("32").append(CRLF).append(CRLF).append("name=%E9%BE%99%E6%9E%97%E5%A4%95");

            Charset UTF_8 = Charset.forName("UTF-8");
            byte[] data = msg.toString().getBytes(UTF_8);
            out.write(data);
            out.flush();  //刷新输出流

            byte[] b = new byte[5 * 1024];  //我这个值其实设置的偏大，对于文本数据来说。
            int len = in.read(b);
            System.out.println(new String(b, 0, len, UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

